/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/
#include "bsp.h"
#include "stdint.h"
#include "os_obj.h"
#include "systask.h"

#include "nor_cmd.h"
#include "extblockflashlog.h"

#include "str_ext.h"
#include "common.h"

/*****************************************************************************
* @brief   extern .
*****************************************************************************/
extern const block_log_cfg_type blog_cfg;
extern const log_buff_cfg_type log_buff_cfg;

const block_log_mix_type log_mix = {
	&blog_cfg,
	&log_buff_cfg};

// 日志数据
typedef struct
{
	sig_time_type time;
	uint16_t temp_pv;
	uint16_t temp_sv;
	uint16_t humi_pv;
	uint16_t humi_sv;
	uint16_t ver[24];
} log_dat_type;

// 日志缓存管理
typedef struct
{
	char init;
	int32_t search_res;
	sig_time_type sig_time;
	log_dat_type log_rd;
	uint16_t read_req;
	uint8_t clr_req;
	int8_t tm_search_req;
	uint8_t hd_search_req;
} log_mix_test_type;

log_mix_test_type log_mix_opt;

/*-----------------------------------------------------------------------------------------------------*
* log_buff_test
*-----------------------------------------------------------------------------------------------------*/
void mix_log_test(void)
{
	// 清除日志缓存
	if (log_mix_opt.clr_req)
	{
	}

	// 读取一条日志
	if (log_mix_opt.read_req)
	{
		block_log_mix_read(&log_mix, log_mix_opt.read_req, 1, &log_mix_opt.log_rd);
		log_mix_opt.read_req = 0;
		dbg_u32_print("mix read :", &log_mix_opt.log_rd, sizeof(log_mix_opt.log_rd) / 4, 40);
	}

	// 搜索
	if (log_mix_opt.tm_search_req)
	{
		block_log_site_type lsite;

		NOP1();
		if (log_mix_opt.tm_search_req < 0)
			log_mix_opt.search_res = block_log_mix_time_search(&log_mix, log_mix_opt.sig_time, -1);
		else
			log_mix_opt.search_res = block_log_mix_time_search(&log_mix, log_mix_opt.sig_time, 1);
		log_mix_opt.tm_search_req = 0;

		if (log_mix_opt.search_res >= 0)
			log_mix_opt.read_req = log_mix_opt.search_res;

		gd25_dbg_print("log buff search res: 0x%x \n", log_mix_opt.search_res);

		lsite = block_log_lsite_to_asite(&blog_cfg, log_mix_opt.search_res);

		gd25_dbg_print("page : %02x, offset :%04x\n", lsite.page, lsite.addr_offset);
	}

	// 搜索
	if (sys_st.log.tm_search_req)
	{
		block_log_site_type lsite;

		NOP1();
		if (sys_st.log.tm_search_req < 0)
			log_mix_opt.search_res = block_log_mix_time_search(&log_mix, sys_st.log.tm_set, -1);
		else
			log_mix_opt.search_res = block_log_mix_time_search(&log_mix, sys_st.log.tm_set, 1);

		sys_st.log.tm_search_req = 0;

		if (log_mix_opt.search_res >= 0)
			log_mix_opt.read_req = log_mix_opt.search_res;

		gd25_dbg_print("log buff search res: 0x%x \n", log_mix_opt.search_res);

		lsite = block_log_lsite_to_asite(&blog_cfg, log_mix_opt.search_res);

		gd25_dbg_print("block : %02x, sub page : %02x, addr offset :%04x\n",
					   lsite.page / log_mix.blk->page_per_block,
					   lsite.page % log_mix.blk->page_per_block,
					   lsite.addr_offset);
	}

	if (sys_st.log.read_amount)
	{
		uu8 buff[32];

		for (int cnt; cnt < sys_st.log.read_amount; cnt++)
		{
			ii32 site = block_log_mix_site_inc(&log_mix, log_mix_opt.search_res, cnt);
			if (site >= 0)
			{
				block_log_mix_read(&log_mix, site, 1, &log_mix_opt.log_rd);
				sig_to_ascii(log_mix_opt.log_rd.time, buff);
				gd25_dbg_print("log start time : %s => ", buff);
				dbg_u32_print("LOG :", &log_mix_opt.log_rd, sizeof(log_mix_opt.log_rd) / 4, 40);
			}
		}

		sys_st.log.read_amount = 0;
	}

	if (log_mix_opt.hd_search_req)
	{
	}
}
